package com.paireach.szwolaiyun.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.fragment.PersonalCenterFragment;
import com.paireach.szwolaiyun.newFragment.HomeFragment;
import com.paireach.szwolaiyun.newFragment.RemoteLoadFragment;
import com.paireach.szwolaiyun.receiver.StatusJumpReceiver;
import com.paireach.szwolaiyun.service.LocationUploadService;
import com.paireach.szwolaiyun.sz_common_widget.activity.BaseActivity;
import com.paireach.szwolaiyun.sz_util.SharedPreferencesUtil;
import com.paireach.szwolaiyun.sz_util.ToastUtil;
import com.paireach.szwolaiyun.util.JPushUtil;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "TAG";
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    //个人中心
    private LinearLayout personalCenter;

    private LinearLayout home;

    private LinearLayout message;

    //个人中心Fragment
    private PersonalCenterFragment personalCenterFragment;

    //HomeFragment
    private HomeFragment homeFragment;

    //多地装货fragment
    private RemoteLoadFragment remoteLoadFragment;

    private StatusJumpReceiver receiver;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initControl();
//        initFragment();
        if (TextUtils.isEmpty(SharedPreferencesUtil.getValue(getApplicationContext(), SharedPreferencesUtil.ALIAS))) {
            JPushInterface.setAlias(getApplicationContext(),
                    SharedPreferencesUtil.getValue(getApplicationContext(), SharedPreferencesUtil.USER_PHONE),
                    new TagAliasCallback() {

                        @Override
                        public void gotResult(int i, String s, Set<String> set) {
                            if (s.equals(SharedPreferencesUtil.getValue(getApplicationContext(), SharedPreferencesUtil.USER_PHONE)) && i == 0) {
                                //绑定成功
                                //保存状态值
                                SharedPreferencesUtil.getValue(getApplicationContext(), SharedPreferencesUtil.ALIAS);
                            } else {
                                //绑定失败
//                                showToast("设置推送失败，请重试");
                            }
                        }
                    });
        }
        receiver = new StatusJumpReceiver(this);
        startService(new Intent(this, LocationUploadService.class));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.paireach.szwolaiyun.ACTION_JUMP");
        registerReceiver(receiver, filter);
    }

    private void initControl() {
        personalCenter = (LinearLayout) findViewById(R.id.personal_center);
        home = (LinearLayout) findViewById(R.id.home);
        message = (LinearLayout) findViewById(R.id.message);
        fragmentManager = getSupportFragmentManager();
        home.setOnClickListener(this);
        personalCenter.setOnClickListener(this);
        message.setOnClickListener(this);
        //默认显示home，模拟被点击
        home.performClick();
    }



    @Override
    public void onClick(View v) {
        fragmentTransaction = fragmentManager.beginTransaction();
        switch (v.getId()) {
            case R.id.personal_center:
                hideAllFragment();
                if (personalCenterFragment == null) {
                    personalCenterFragment = new PersonalCenterFragment();
                    fragmentTransaction.add(R.id.change_layout, personalCenterFragment, "RemoteLoadFragment");
                }
                fragmentTransaction.show(personalCenterFragment);
                break;
            case R.id.home:
                hideAllFragment();
                if (homeFragment == null) {
                    homeFragment = HomeFragment.getInstance(
                            SharedPreferencesUtil.getValue(this, SharedPreferencesUtil.USER_NAME),
                            SharedPreferencesUtil.getValue(this, SharedPreferencesUtil.USER_PHONE));
                    fragmentTransaction.add(R.id.change_layout, homeFragment, "HomeFragment");
                }
                fragmentTransaction.show(homeFragment);
                break;
            case R.id.message:
                hideAllFragment();
                if (remoteLoadFragment == null) {
                    remoteLoadFragment = new RemoteLoadFragment();
                    fragmentTransaction.add(R.id.change_layout, remoteLoadFragment, "RemoteLoadFragment");
                }
                fragmentTransaction.show(remoteLoadFragment);
                break;
        }
        fragmentTransaction.commit();
    }
    //隐藏所有的fragment
    public void hideAllFragment() {
        if (homeFragment != null) {
            fragmentTransaction.hide(homeFragment);
        }
        if (remoteLoadFragment != null) {
            fragmentTransaction.hide(remoteLoadFragment);
        }
        if (personalCenterFragment != null) {
            fragmentTransaction.hide(personalCenterFragment);
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private class StatusJumpReceiver extends BroadcastReceiver {

        private String action = "com.paireach.szwolaiyun.ACTION_JUMP";
        private String orderNumber;
        private Activity mContext;

        public StatusJumpReceiver(Activity mContext) {
            this.mContext = mContext;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(action)) {
                mContext.removeStickyBroadcast(new Intent("com.paireach.szwolaiyun.ACTION_JUMP"));
                //解析数据
                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                    orderNumber = bundle.getString("orderCode");
                    Intent intent1 = new Intent(MainActivity.this, Activity_B.class);
                    intent1.putExtra("orderNumber", orderNumber);
                    startActivity(intent1);
                }
            }
        }
    }

}
